//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // let token = '637ca2fa5590681aa707253a712279c4';
    // wx.setStorageSync('token', token);
    var that = this;
    wx.checkSession({
        success: function () {
          //session_key 未过期，并且在本生命周期一直有效
        },
        fail: function () {
          // session_key 已经失效，需要重新执行登录流程
          wx.login({
            success: res => {
              // 发送 res.code 到后台换取 openId, sessionKey, unionId
              var code = res.code;
              // console.log(code)
              wx.request({
                url: that.globalData.directory+'api/v2/token/user',
                method: 'POST',
                data: {
                  code: code
                },
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                success: function (res) {
                  if(res.data.status=='ok'){
                    // let token = '637ca2fa5590681aa707253a712279c4';
                    wx.setStorageSync('token',res.data.data);
                    that.denglu1();
                    that.couponpoints();
                  }else{
                    wx.showToast({
                      title: '请求失败,请重试',
                      icon: 'none',
                      duration: 2000,
                    })
                  }
                }
              })
            }
          }) //重新登录
        }
    })
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var code = res.code;
        wx.request({
          url: that.globalData.directory+'api/v2/token/user',
          method: 'POST',
          data:{
            code:code
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log("101", res)
            if (res.data.status == 'ok') {
              // let token = '637ca2fa5590681aa707253a712279c4';
              wx.setStorageSync('token', res.data.data);
              that.denglu1();
              that.couponpoints();
            } else {
              wx.showToast({
                title: '请求失败,请重试',
                icon: 'none',
                duration: 2000,
              })
            }
          }
        })
      }
    })    
  },
  login(userinfo, callback) {
    var that=this;
    console.log(userinfo);
    if (userinfo.detail.errMsg == 'getUserInfo:ok') {
      var userinfo1 = userinfo.detail.userInfo;
      wx.request({
        url: that.globalData.directory + 'api/v2/user/getinfo',
        method: 'POST',
        data: {
          data: userinfo1
        },
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token')
        },
        success: function (res) {
          that.denglu1();
          callback(false, null)
        },
      })
    }
    else if (userinfo.detail.errMsg == 'getUserInfo:fail auth deny') { // 当用户点击拒绝时
      wx.showModal({
        title:'不授权无法正常使用功能'
      }) // 提示用户，需要授权才能登录
      callback('fail to modify scope', null)
    }
  },
  /*判断积分优惠券是否能够使用 */
  couponpoints:function(){
    var that=this;
    wx.request({
      url: that.globalData.directory + 'api/v2/pay/facepay',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.errMsg == 'request:ok') {
          // that.globalData.coupon = res.data.coupon;
          that.globalData.points = res.data.points;
        } else {
          wx.showToast({
            title: '请求失败,请重试',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
  },
  denglu1: function (userinfo1) {
    var that = this;
    wx.request({
      url: that.globalData.directory + 'api/v2/user/getuser',
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if(res.data.status=='ok'){
          that.globalData.user = res.data.data;
          // wx.request({
          //   url: that.globalData.directory + 'api/v2/pay/facepay',
          //   header: {
          //     'content-type': 'application/json',
          //     'token': wx.getStorageSync('token')
          //   },
          //   success: function (res) {
          //     if (res.errMsg == 'request:ok') {
          //       that.globalData.coupon = res.data.coupon;
          //       that.globalData.points = res.data.points;
          //     } else {
          //       wx.showToast({
          //         title: '请求失败,请重试',
          //         icon: 'none',
          //         duration: 2000,
          //       })
          //     }
          //   }
          // })
        }
      },
    })
  },
  globalData: {
    userInfo: null,
    directory: 'http://192.168.0.74/',
    user: {},//用户
    choosewelfare:'',//选择的优惠券
    coupon:{},//优惠券
    points:{},//积分
    tableid:'',//桌号
  }
})